/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.tenant.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.enums.DeletedEnum;
import net.siufung.core.page.PagerResult;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.tenant.controller.assembler.TenantTypeAssembler;
import net.siufung.market.base.tenant.controller.model.dto.TenantTypeDto;
import net.siufung.market.base.tenant.controller.model.dto.TenantTypeSaveDto;
import net.siufung.market.base.tenant.controller.model.dto.TenantTypeUpdateDto;
import net.siufung.market.base.tenant.controller.model.dto.page.TenantTypePageDto;
import net.siufung.market.base.tenant.controller.model.vo.TenantTypeVo;
import net.siufung.market.base.tenant.controller.regulation.TenantTypeCreateRegulation;
import net.siufung.market.base.tenant.controller.regulation.TenantTypeDeleteRegulation;
import net.siufung.market.base.tenant.controller.regulation.TenantTypeStatusRegulation;
import net.siufung.market.base.tenant.controller.regulation.TenantTypeUpdateRegulation;
import net.siufung.market.base.tenant.mapper.ITenantMapper;
import net.siufung.market.base.tenant.mapper.ITenantTypeMapper;
import net.siufung.market.base.tenant.mapper.entity.TenantType;
import net.siufung.market.base.tenant.service.ITenantTypeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统租户类型表 前端控制器
 *
 * @author 陈建峰
 * @since 2022-07-06 14:45:08
 */
@Api(tags = "系统租户类型表")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/tenant/type")
public class TenantTypeController {

    private final ITenantTypeMapper tenantTypeMapper;
    private final ITenantTypeService tenantTypeService;
    private final ITenantMapper tenantMapper;

    /**
     * 通过主键获取 系统租户类型表
     * @param id 主键
     * @return Result<TenantTypeVo>
     * */
    @ApiOperation(value = "通过Id获取系统租户类型表信息")
    @GetMapping(value = "/id/{id}")
    public Result<TenantTypeVo> getById(
            @PathVariable("id") Integer id) {
        return Result.ok(
            TenantTypeAssembler.from(tenantTypeMapper.selectById(id)));
    }

    /**
     * 获取 系统租户类型表列表
     * @param tenantTypeDto 属性
     * @return Result<List<TenantTypeVo>>
     * */
    @ApiOperation(value = "获取系统租户类型表列表")
    @PostMapping(value = "/list")
    public Result<List<TenantTypeVo>> list(
            @RequestBody TenantTypeDto tenantTypeDto) {
        return Result.ok(
            TenantTypeAssembler.fromList(tenantTypeMapper.selectList(Wrappers.query(tenantTypeDto.to()))));
    }

    /**
     * 获取 系统租户类型表分页
     * @param tenantTypePageDto 属性
     * @return 查询分页结果
     * */
    @ApiOperation(value = "获取系统租户类型表分页")
    @PostMapping(value="/page")
    public Result<PagerResult<TenantTypeVo>> getPage(
            @RequestBody TenantTypePageDto tenantTypePageDto) {
        tenantTypePageDto.validated();
        QueryWrapper<TenantType> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(!Strings.isNullOrEmpty(tenantTypePageDto.getName()) ,
                TenantType::getName, tenantTypePageDto.getName());
        wrapper.lambda().orderByDesc(TenantType::getId);
        IPage<TenantType> tenantTypePage = tenantTypeMapper.selectPage(
                new Page<>(tenantTypePageDto.getPageNo(), tenantTypePageDto.getPageSize()), wrapper);
        PagerResult<TenantTypeVo> tenantTypeVoPage = new PagerResult<>(
                tenantTypePage.getCurrent(), tenantTypePage.getSize(), tenantTypePage.getTotal());
        tenantTypeVoPage.setRecords(
            TenantTypeAssembler.fromList(tenantTypePage.getRecords()));
        return Result.ok(tenantTypeVoPage);
    }

    /**
     * 通过主键删除 系统租户类型表 对象
     * @param id 主键
     * @return 删除结果
     * */
    @ApiOperation(value = "删除系统租户类型表对象")
    @PostMapping(value="/remove/{id}")
    public Result<Void> deleteById(
        @PathVariable("id") Integer id) {
        TenantTypeDeleteRegulation tenantTypeDeleteRegulation = new TenantTypeDeleteRegulation(tenantTypeMapper, tenantMapper);
        tenantTypeDeleteRegulation.isSatisfiedBy(id);
        if(tenantTypeService.removeById(id)){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 创建 系统租户类型表
     * @param tenantTypeSaveDto 对象属性
     * @return 创建结果
     * */
    @ApiOperation(value = "创建 系统租户类型表")
    @PostMapping(value="/create")
    public Result<Void> create(
            @RequestBody TenantTypeSaveDto tenantTypeSaveDto) {
        tenantTypeSaveDto.validated();
        TenantType tenantType = tenantTypeSaveDto.to();
        TenantTypeCreateRegulation tenantTypeCreateRegulation = new TenantTypeCreateRegulation(tenantTypeMapper);
        tenantTypeCreateRegulation.isSatisfiedBy(tenantType);
        if(tenantTypeService.save(tenantType)){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }

    /**
     * 更新 系统租户类型表
     * @param tenantTypeUpdateDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 系统租户类型表 信息")
    @PostMapping(value="/update")
    public Result<Void> updateById(
            @RequestBody TenantTypeUpdateDto tenantTypeUpdateDto) {
        tenantTypeUpdateDto.validated();
        TenantType tenantTypeOf = tenantTypeService.getById(tenantTypeUpdateDto.getId());
        CopyUtil.copyPropertiesIgnoreNull(tenantTypeUpdateDto.to(), tenantTypeOf);
        TenantTypeUpdateRegulation tenantTypeUpdateRegulation = new TenantTypeUpdateRegulation(tenantTypeMapper);
        tenantTypeUpdateRegulation.isSatisfiedBy(tenantTypeOf);
        if(tenantTypeService.updateById(tenantTypeOf)){
            return Result.ok(ResultOk.UPDATE);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }

    /**
     * 更新 系统租户类型表 状态
     * @param id 主键
     * @param deleted 数据状态
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 系统租户类型表 状态")
    @PostMapping(value="/status/{id}/{deleted}")
    public Result<Void> updateStatusById(
            @PathVariable("id") Integer id,
            @PathVariable("deleted") boolean deleted) {
        TenantTypeStatusRegulation tenantStatusRegulation = new TenantTypeStatusRegulation(tenantTypeMapper);
        tenantStatusRegulation.isSatisfiedBy(id);
        if(tenantTypeService.updateStatusById(id, DeletedEnum.get(deleted))){
            return Result.ok(ResultOk.STATUS);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }

}